<template>
  <el-dialog :title="title" :visible.sync="isShow" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="openEvent">
    <el-form :model="form">
      <el-form-item label="公司名称" :label-width="formLabelWidth">
        <el-input v-model="form.companyName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地址" :label-width="formLabelWidth">
        <el-input v-model="form.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="邮政编码" :label-width="formLabelWidth">
            <el-input v-model="form.postCode" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" label-width="80px">
            <el-radio-group v-model="form.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="排序" label-width="80px">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="operateAppInfo">确 定</el-button>
    </div>
  </el-dialog>
</template>
    
<script>
import { addCompany, updateCompany } from '@/api/company'

export default {
  name: 'CompanyInfoComp',
  data () {
    return {
      form: {
        companyName: '',
        address: '',
        postCode: '',
        remark: "",
        status: "0",
        sort: 0
      },
      formLabelWidth: '120px',
      title: '新增公司'
    }
  },
  props: {
    isShow: {
      type: Boolean,
      required: true
    },
    item: {
      type: Object,
      require: false
    }
  },
  methods: {
    openEvent () {
      if (this.item === undefined) {
        this.form.companyName = ""
        this.form.address = ""
        this.form.postCode = ""
        this.form.remark = ""
        this.form.status = "0"
        this.form.sort = 0
        this.title = '新增公司'
      } else {
        this.title = '编辑公司'
        this.form = { ...this.item }
      }
    },
    closeDialog () {
      this.$emit("changeDialogStatus", false);
    },
    result (data) {
      this.$emit("changeDialogStatus", "success");
      if (data.status === 200) {
        this.$message.success("操作成功");
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    operateAppInfo () {
      const param = { ...this.form }
      if (this.item === undefined) {
        addCompany(param).then((res) => {
          this.result(res)
        });
      } else {
        updateCompany(param).then((res) => {
          this.result(res)
        });
      }
    }
  }
}
</script>
    
<style scoped>
</style>
    